package br.ufpe.cin.rva;

import javax.sound.midi.Instrument;
import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Receiver;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Soundbank;
import javax.sound.midi.Synthesizer;

public class PlayNote extends Thread {
	ShortMessage msg;
	Synthesizer synth;
	Receiver rcvr;
	Sequencer sequencer;
	Sequence sequence;
	long timeStamp;
	int nota;
	Instrument instruments[];
	ChannelData channels[];
	ChannelData cc;
	
	public int getNota() {
		return nota;
	}

	public void setNota(int nota) {
		this.nota = nota;
	}

	PlayNote() {
		super();
		this.nota = nota;
		try {
			synth = MidiSystem.getSynthesizer();
			synth.open();
			sequencer = MidiSystem.getSequencer();
			sequence = new Sequence(Sequence.PPQ, 10);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Soundbank sb = synth.getDefaultSoundbank();
		if (sb != null) {
			instruments = synth.getDefaultSoundbank().getInstruments();
			synth.loadInstrument(instruments[0]);
		}
		MidiChannel midiChannels[] = synth.getChannels();
		channels = new ChannelData[midiChannels.length];
		for (int i = 0; i < channels.length; i++) {
			channels[i] = new ChannelData(midiChannels[i], i);
		}
		cc = channels[0];
	}

	public void run() {
		cc.channel.noteOn(nota, cc.velocity);
	}
	
	class ChannelData {
		MidiChannel channel;
		int velocity, pressure, bend, reverb;
		int row, col, num;
		
		public ChannelData(MidiChannel channel, int num) {
			this.channel = channel;
			this.num = num;
			velocity = pressure = bend = reverb = 64;
		}
	}
}